package com.cz.rabbitmq.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cz.rabbitmq.dto.DoNextDTO;
import com.cz.rabbitmq.dto.StartFLowDTO;
import com.rabbitmq.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author chenzhen
 * @time 2022/11/14
 * @desc
 */

@Service
public class MsgService {
    @Autowired
    private ConnectionFactory connectionFactory;

    public void sendMsg(){
        try (Connection connection = connectionFactory.newConnection();Channel channel=connection.createChannel()) {
            StartFLowDTO startFLowDTO = new StartFLowDTO();
            startFLowDTO.setUserId("1");
            startFLowDTO.setSubject("我的分支测试cz6");
            startFLowDTO.setFlowAction("start");
            startFLowDTO.setBizType("cz");
            startFLowDTO.setBusinessKey("6");
            startFLowDTO.setFlowKey("mykeytest");
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("name","cz");
            dataMap.put("age","cz1");
            startFLowDTO.setExtraData(dataMap);

            AMQP.BasicProperties basicProperties = new AMQP.BasicProperties.Builder().deliveryMode(2).contentType("application/json").contentEncoding("utf-8").build();
            channel.basicPublish("bus.topic.exchange","bus.topic.workflow.cz",basicProperties, StrUtil.bytes(JSONUtil.toJsonStr(startFLowDTO),"utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }


    public void sendNextMsg(){
        try (Connection connection = connectionFactory.newConnection();Channel channel=connection.createChannel()) {
            DoNextDTO doNextDTO = new DoNextDTO();
            doNextDTO.setActionName("agree");
            doNextDTO.setTaskId("1596116687208701955");
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("myvars1 ",1);
            //doNextDTO.setNodeVars(dataMap);
            doNextDTO.setVars(dataMap);
            doNextDTO.setFlowAction("next");
            doNextDTO.setUserId("1");
            AMQP.BasicProperties basicProperties = new AMQP.BasicProperties.Builder().deliveryMode(2).contentType("application/json").contentEncoding("utf-8").build();
            channel.basicPublish("bus.topic.exchange","bus.topic.workflow.cz",basicProperties, StrUtil.bytes(JSONUtil.toJsonStr(doNextDTO),"utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

    public void receiveMsg(){
        try (Connection connection = connectionFactory.newConnection();Channel channel=connection.createChannel()) {
            channel.queueBind("bus.topic.workflow", "bus.topic.exchange","bus.topic.workflow");

            channel.basicConsume("bus.topic.workflow", true, "", new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println(StrUtil.str(body,"utf-8"));
                }

            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
